package com.kk.base.controller.admin;

import com.kk.base.bean.CodeMsg;
import com.kk.base.bean.PageBean;
import com.kk.base.bean.Result;
import com.kk.base.entity.home.HomeUser;
import com.kk.base.service.home.HomeUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 后台查看前台用户控制层
 */
@RequestMapping("/admin/homeUser")
@Controller
public class BackHomeUserController {

    @Autowired
    private HomeUserService homeUserService;


    /**
     * 前台用户列表页面
     * @param model
     * @param homeUser
     * @param pageBean
     * @return
     */
    @RequestMapping(value="/list")
    public String list(Model model, HomeUser homeUser, PageBean<HomeUser> pageBean){
        model.addAttribute("title", "前台用户列表");
        model.addAttribute("mobile", homeUser.getMobile());
        model.addAttribute("pageBean", homeUserService.findList(homeUser, pageBean));
        return "admin/home_user/list";
    }


    /**
     * 前台用户编辑页面
     * @param model
     * @return
     */
    @RequestMapping(value="/edit",method= RequestMethod.GET)
    public String edit(Model model,@RequestParam(name="id")Long id){
        model.addAttribute("homeUser", homeUserService.find(id));
        return "admin/home_user/edit";
    }

    /**
     * 编辑前台用户状态
     * @param homeUser
     * @return
     */
    @RequestMapping(value="/edit",method=RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> edit(HomeUser homeUser){
        HomeUser findById = homeUserService.find(homeUser.getId());
        findById.setStatus(homeUser.getStatus());
        if(homeUserService.save(findById) == null){
            return Result.error(CodeMsg.HOMEUSER_USER_EDIT_ERROR);
        }
        return Result.success(true);
    }


    
}
